package com.easemob.server.example.utils;

import org.framework.common.ext.util.StringUtil;

import com.easemob.server.example.api.ChatGroupAPI;
import com.easemob.server.example.api.IMUserAPI;
import com.easemob.server.example.comm.ClientContext;
import com.easemob.server.example.comm.EasemobRestAPIFactory;
import com.easemob.server.example.comm.body.IMUserBody;
import com.easemob.server.example.comm.body.ModifyNicknameBody;
import com.easemob.server.example.comm.body.ResetPasswordBody;
import com.easemob.server.example.comm.body.UserNamesBody;
import com.easemob.server.example.comm.wrapper.BodyWrapper;
import com.easemob.server.example.comm.wrapper.ResponseWrapper;

public class EasemobUtils {

	private static IMUserAPI userapi;
	private static ChatGroupAPI groupapi;
	
	static{
		EasemobRestAPIFactory factory = ClientContext.getInstance().init(ClientContext.INIT_FROM_PROPERTIES).getAPIFactory();
		userapi = (IMUserAPI)factory.newInstance(EasemobRestAPIFactory.USER_CLASS);
		groupapi = (ChatGroupAPI)factory.newInstance(EasemobRestAPIFactory.CHATGROUP_CLASS);
	}
	
	/**
	 * 添加群组成员.
	 * @param groupid
	 * @param users
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年6月27日 下午2:40:59
	 */
	public static boolean appendGroups(final String groupid, final String[] users){
		UserNamesBody body = new UserNamesBody(users);
		ResponseWrapper result = (ResponseWrapper)groupapi.addBatchUsersToChatGroup(groupid, body);
		return result.getResponseStatus() == 200;
	}
	
	/**
	 * 删除群组成员.
	 * @param groupid
	 * @param users
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年6月27日 下午2:42:08
	 */
	public static boolean removeGroups(final String groupid, final String[] users){
		ResponseWrapper result = (ResponseWrapper)groupapi.removeBatchUsersFromChatGroup(groupid, users);
		return result.getResponseStatus() == 200;
	}
	
	/**
	 * 新增环信账号.
	 * @param id
	 * @param password
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年5月9日 下午4:08:48
	 */
	public static boolean createIMUser(final String id, final String password, final String nick){
        BodyWrapper userBody = new IMUserBody(id, password, StringUtil.isEmpty(nick) ? "匿名" : nick);
        
        // 注册环信帐户
        ResponseWrapper response = userapi.createNewIMUserSingle(userBody);
        
        return response.getResponseStatus() == 200;
	}
	
	
	/**
	 * 根据用户名查找环信用户.
	 * @param userName
	 * @return
	 * @author Administrator(zhaochong)
	 * @date 2016年5月11日 上午9:52:51
	 */
	public static boolean getIMUser(final String userName){
		ResponseWrapper response = userapi.getIMUsersByUserName(userName);
		return response.getResponseStatus() == 200;
	}
	
	/**
	 * 删除用户名环信用户.
	 * @param userName
	 * @return
	 * @author Administrator(zhaochong)
	 * @date 2016年5月11日 上午9:52:51
	 */
	public static boolean deleteIMUser(final String userName){
		ResponseWrapper response = userapi.deleteIMUserByUserName(userName);
        return response.getResponseStatus() == 200;
	}
	
	/**
	 * 修改环信账号.
	 * @param id
	 * @param password
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年5月9日 下午4:09:07
	 */
	public static boolean resetPassword(final String id, final String password, final String nick){
		// 返回用户信息
		if(getIMUser(id)){
			BodyWrapper payload = new ResetPasswordBody(password);
			ResponseWrapper response = userapi.modifyIMUserPasswordWithAdminToken(id, payload);
			return response.getResponseStatus() == 200;
		}else{
			return createIMUser(id, password, nick);
		}
	}
	
	/**
	 * 修改环信昵称.
	 * @param id
	 * @param password
	 * @return
	 * @author chanlong(陈龙)
	 * @date 2016年5月9日 下午4:09:07
	 */
	public static boolean modifyNickname(final String id, final String nick){
		// 返回用户信息
		if(getIMUser(id)){
			BodyWrapper payload = new ModifyNicknameBody(nick);
			ResponseWrapper response = userapi.modifyIMUserNickNameWithAdminToken(id, payload);
			return response.getResponseStatus() == 200;
		}else{
			return false;
		}
	}
}
